/**
 * 生成csv文件
 * @param {*} filename 生成文件名
 * @param {*} data 生成数据
 */
const dowlondCsv = (filename, data) => {
  if (!Array.isArray(data)) {
    return;
  }
  let str = data
    .map(row => {
      if (typeof row === 'string') {
        return row;
      }
      if (Array.isArray(row)) {
        return row.join(',');
      }
      return null;
    })
    .filter(Boolean)
    .join('\n');
  let uri = 'data:text/csv;charset=utf-8,' + encodeURIComponent(str);
  let link = document.createElement('a');
  link.href = uri;
  link.download = /\.csv$/.test(filename) ? filename : `${filename}.csv`;
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
};
export default dowlondCsv;
